/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.analyse;

import java.util.LinkedList;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class Main {

    private static List<CardSet> listCardSets = new LinkedList<CardSet>();
    private static final int[] card6Phom = {7, 8, 9, 20, 33, 21, 34, 22, 35};
    private static final int[] card4Phom = {7, 8, 9, 20, 21, 22, 35, 34};
    private static final int[] card4Phom2 = {7, 20, 33, 8, 9, 21, 22, 34, 35};//==card6
    private static final int[] card4Phom3 = {7, 8, 9, 20, 33, 21, 34, 22, 35};
    private static final int[] card5Phom = {7, 8, 9, 20, 21, 22, 35, 34, 36};

    public static void main(String[] args) {

        CardDealer dealer = new CardDealer();
        dealer.shuffleCard();
        int numberCard = 52;
        int[] listCards = new int[numberCard];
        for (int i = 0; i < numberCard; i++) {
            listCards[i] = dealer.nextCard();
        }

        listCards = card5Phom;

        Debug.d("flush:" + checkFlush(listCards));
//        Debug.d("pair:" + checkPair(listCards));
        printListCardId(listCards);
        Debug.d("=====================");
        printCard(listCards);
        Debug.d("=====================");
        extractPair(listCards);
        extractFlush(listCards);
        Debug.d("set size :" + listCardSets.size());
        for (CardSet s : listCardSets) {
            Debug.n(s.getTypeName() + ":" + s.toString());
            Debug.d("");
        }
    }

    private static void printListCardId(int[] listCards) {
        int numberCard = listCards.length;
        for (int i = 0; i < numberCard; i++) {
            System.out.print(listCards[i] + ",");

        }
    }

    private static void extractSpecialFlush(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] / 13 > listCards[j] / 13
                        || (listCards[i] / 13 == listCards[j] / 13
                        && listCards[i] % 13 > listCards[j] % 13)) {
                    swap(i, j, listCards);
                }
            }
        }
        for (int i = 0; i < numberCards; i++) {
            int step = 0;
            int cardId = listCards[i];
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId + (j - i) == listCards[j]) {
                    step++;
                } else {
                    j = numberCards;
                }
            }
            
            if (step != 0) {
                int[] flsush = new int[step + 1];
                for (int k = i; k <= i + step; k++) {
                    flsush[k - i] = listCards[k];
                }
                if (step >= 2) {
                    CardSet flushSet = new CardSet(flsush, CardSet.FLUSH);
                    listCardSets.add(flushSet);
                }
                i = i + step;
            }

        }
    }

    private static void extractFlush(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] / 13 > listCards[j] / 13
                        || (listCards[i] / 13 == listCards[j] / 13
                        && listCards[i] % 13 > listCards[j] % 13)) {
                    swap(i, j, listCards);
                }
            }
        }
        for (int i = 0; i < numberCards; i++) {
            int step = 0;
            int cardId = listCards[i];
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId + (j - i) == listCards[j] && cardId / 13 == listCards[j] / 13) {
                    step++;
                } else {
                    j = numberCards;
                }
            }
            if (step != 0) {
//                Debug.d("day :" + step);
                int[] flsush = new int[step + 1];
                for (int k = i; k <= i + step; k++) {
                    flsush[k - i] = listCards[k];
//                    Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                }
                if (step >= 2) {
                    CardSet flushSet = new CardSet(flsush, CardSet.FLUSH);
                    listCardSets.add(flushSet);
                }
//                Debug.d("");
                i = i + step;
            }

        }
    }

    private static void swap(int i, int j, int[] listCards) {
        int temp = listCards[j];
        listCards[j] = listCards[i];
        listCards[i] = temp;
    }

    private static void extractPair(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] % 13 > listCards[j] % 13) {
                    int temp = listCards[j];
                    listCards[j] = listCards[i];
                    listCards[i] = temp;
                }
            }
        }

        printListCardId(listCards);
        Debug.d("");
        printCard(listCards);
        //tim phom ngang
        for (int i = 0; i < numberCards; i++) {
            int cardId = listCards[i];
            int step = 0;
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId % 13 == listCards[j] % 13) {
                    if (j < numberCards - 1 && cardId % 13 == listCards[j + 1] % 13) {
                        if (j < numberCards - 2 && cardId % 13 == listCards[j + 2] % 13) {
//                            Debug.d("bo bon");
                            step = 3;
                        } else {
//                            Debug.d("bo ba");
                            step = 2;
                        }
                    } else {
//                        Debug.d("bo 2");
                        step = 1;
                    }
                    int[] pair = new int[step + 1];
                    for (int k = i; k <= i + step; k++) {
                        pair[k - i] = listCards[k];
//                        Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                    }
                    if (step >= 2) {
                        CardSet pairSet = new CardSet(pair, CardSet.PAIR);
                        listCardSets.add(pairSet);
                    }

//                    Debug.d("");
                    j = numberCards;
                    i = i + step;
                }
            }
        }
    }

    private static void printCard(int[] listCards) {
        for (Integer i : listCards) {
            Debug.n(Card.getRank(i) + Card.getSuit(i));
        }
        System.out.println();
    }

    private static boolean checkPair(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] % 13 > listCards[j] % 13) {
                    int temp = listCards[j];
                    listCards[j] = listCards[i];
                    listCards[i] = temp;
                }
            }
        }

        for (int i = 0; i < numberCards; i++) {
            int cardId = listCards[i];
            int step = 0;
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId % 13 == listCards[j] % 13) {
                    if (j < numberCards - 1 && cardId % 13 == listCards[j + 1] % 13) {
                        if (j < numberCards - 2 && cardId % 13 == listCards[j + 2] % 13) {
//                            Debug.d("bo bon");
                            step = 3;
                        } else {
//                            Debug.d("bo ba");
                            step = 2;
                        }
                    } else {
//                        Debug.d("bo 2");
                        step = 1;
                    }
                    int[] pair = new int[step + 1];
                    for (int k = i; k <= i + step; k++) {
                        pair[k - i] = listCards[k];
                        Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                    }
//                    Debug.d("check pair :" + step);
                    if (step >= 2) {
                        return true;
                    }
//                    Debug.d("");
                    j = numberCards;
                    i = i + step;
                }
            }
        }
        return false;
    }

    private static boolean checkFlush(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] / 13 > listCards[j] / 13
                        || (listCards[i] / 13 == listCards[j] / 13
                        && listCards[i] % 13 > listCards[j] % 13)) {
                    swap(i, j, listCards);
                }
            }
        }
        for (int i = 0; i < numberCards; i++) {
            int step = 0;
            int cardId = listCards[i];
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId + (j - i) == listCards[j] && cardId / 13 == listCards[j] / 13) {
                    step++;
                } else {
                    j = numberCards;
                }
            }
            if (step != 0) {
//                Debug.d("day :" + step);
                int[] flsush = new int[step + 1];
                for (int k = i; k <= i + step; k++) {
                    flsush[k - i] = listCards[k];
                    Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                }
                if (step >= 2) {
                    Debug.d("check flush :" + step);
                    return true;
                }
                i = i + step;
            }
        }
        return false;
    }
}
